﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;

namespace Light
{
    /// <summary>
    /// 马路。保存所有的车和交通灯。
    /// 每辆车会保存road的引用以便于查询自己的马路状态而调整自己的行为。
    /// Road相当于全局数据区。
    /// </summary>
    partial class Road : UIElement
    {
        #region 属性
        List<Car> Cars = new List<Car>();
        List<TrafficLight> Lights = new List<TrafficLight>();

        public List<Car> CarList
        {
            get { return Cars; }
        }

        public List<TrafficLight> LightList
        {
            get { return Lights; }
        }
        #endregion

        #region 车辆管理相关

        public List<List<Car>> laneCar = new List<List<Car>>( 12 );

        public void AddCar( Car car )
        {
            laneCar[ car.Lane ].Add( car );
            Cars.Add( car );
        }

        public void RemoveCar( Car car )
        {
            Cars.Remove( car );
        }

        public void AddLight( TrafficLight light )
        {
            Lights.Add( light );
        }

        public void RemoveLight( TrafficLight light )
        {
            Lights.Remove( light );
        }

        // 在某条路上加入随机车辆
        public void AddRandomCar( int road )
        {

        }

        #endregion

        public Road( Canvas p )
        {
            parentCanvas = p;

            for ( int i = 0; i < 12; i++ )
            {
                List<Car> l = new List<Car>();
                laneCar.Add( l );
            }
        }


        int count = 0;
        int time_left = 5;
        int time_flash = 2;
        int time_yellow = 1;
        int flag = 1;
        int work = 1;
        //int time_left_count = 0;
        // 每个50ms被调用一次
        public override void OnAnimate()
        {
            // 通知交通灯
            foreach ( TrafficLight light in Lights )
            {
                light.OnAnimate();
            }

            // 通知汽车
            foreach ( Car car in Cars )
            {
                car.OnAnimate();
            }

            //if ( time_left_count % 20 == 0 )
            //{
            //    time_left--;
            //}
            //time_left_count++;

            /// 自己的处理函数
            /// 

         //   count++;
           /*
             int temp = 0;
                        int max = 0;
                        if ( flag == 1 )
                        {
                            for ( int i = 0; i < 12; i++ )
                            {
            
                                if ( i % 3 != 0 )
                                {
                                    if ( max < laneCar[ i ].Count )
                                    {
            
                                        max = laneCar[ i ].Count;
                                        temp = i;
                                    }
                                }
                            }
            
                            if ( temp % 3 ==1 && work == 1)
                            {
                                int save_temp = temp  / 3;
                                Lights[ save_temp ].Status = LightStatus.STRAIGHT;
                                Lights[ ( save_temp + 2 ) % 4 ].Status = LightStatus.STRAIGHT;
                                Lights[ ( save_temp + 1 ) % 4 ].Status = LightStatus.RED;
                                Lights[ ( save_temp + 3 ) % 4 ].Status = LightStatus.RED;
                                Lights[ 0 ].SetTime( time_left );
                                Lights[ 1 ].SetTime( time_left );
                                Lights[ 2 ].SetTime( time_left );
                                Lights[ 3 ].SetTime( time_left );
                                work = 0;
                            }
            
                            else if ( temp % 3 == 2 && work == 1 )
                            {
                                int save_temp = temp  / 3;
                                Lights[ save_temp ].Status = LightStatus.LEFT;
                                Lights[ ( save_temp + 2 ) % 4 ].Status = LightStatus.LEFT;
                                Lights[ ( save_temp + 1 ) % 4 ].Status = LightStatus.RED;
                                Lights[ ( save_temp + 3 ) % 4 ].Status = LightStatus.RED;
                                Lights[ 0 ].SetTime( time_left );
                                Lights[ 1 ].SetTime( time_left );
                                Lights[ 2 ].SetTime( time_left );
                                Lights[ 3 ].SetTime( time_left );
                                work = 0;
                            }
                        }
                        if( flag == 2 )
                        {
                            if ( work == 0 )
                            {
                            int save_temp = temp / 3;
                            Lights[ save_temp ].Status = LightStatus.YELLOW;
                            Lights[ ( save_temp + 2 ) % 4 ].Status = LightStatus.YELLOW;
                            Lights[ ( save_temp + 1 ) % 4 ].Status = LightStatus.RED;
                            Lights[ ( save_temp + 3 ) % 4 ].Status = LightStatus.RED;
                            Lights[ 0 ].SetTime( time_yellow );
                            Lights[ 1 ].SetTime( time_yellow );
                            Lights[ 2 ].SetTime( time_yellow );
                            Lights[ 3 ].SetTime( time_yellow );
                            work = 1;
                            }
            
            
                        }*/
            

        }

        int time_red = 20;

        public void OnTimeZero( int i )
        {
            flag += 1;
            flag %= 3;


            if ( true )
            {
                switch ( count )
                {
                    case 0:
                        Lights[ 0 ].Status = LightStatus.STRAIGHT;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.STRAIGHT;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_left + 1 - 1 );
                        Lights[ 1 ].SetTime( time_red + 1 );
                        Lights[ 2 ].SetTime( time_left + 1 );
                        Lights[ 3 ].SetTime( time_red + 1 );
                        count = 1;
                        break;
                    case 1:
                        Lights[ 0 ].Status = LightStatus.STRAIGHT_FLASH;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.STRAIGHT_FLASH;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_flash + 1 - 1 );
                        Lights[ 2 ].SetTime( time_flash + 1 );
                        count = 2;
                        break;
                    case 2:
                        Lights[ 0 ].Status = LightStatus.YELLOW;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.YELLOW;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_yellow + 1 - 1 );
                        Lights[ 2 ].SetTime( time_yellow + 1 );
                        count = 3;
                        break;
                    case 3:
                        Lights[ 0 ].Status = LightStatus.YELLOW_FLASH;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.YELLOW_FLASH;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_flash + 1 - 1 );
                        Lights[ 2 ].SetTime( time_flash + 1 );
                        count = 4;
                        break;
                    case 4:
                        Lights[ 0 ].Status = LightStatus.LEFT;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.LEFT;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_left + 1 - 1 );
                        Lights[ 2 ].SetTime( time_left + 1 );
                        count = 5;
                        break;
                    case 5:
                        Lights[ 0 ].Status = LightStatus.LEFT_FLASH;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.LEFT_FLASH;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_flash + 1 - 1 );
                        Lights[ 2 ].SetTime( time_flash + 1 );
                        count = 6;
                        break;
                    case 6:
                        Lights[ 0 ].Status = LightStatus.YELLOW;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.YELLOW;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_yellow + 1 - 1 );
                        Lights[ 2 ].SetTime( time_yellow + 1 );
                        count = 7;
                        break;
                    case 7:
                        Lights[ 0 ].Status = LightStatus.YELLOW_FLASH;
                        Lights[ 1 ].Status = LightStatus.RED;
                        Lights[ 2 ].Status = LightStatus.YELLOW_FLASH;
                        Lights[ 3 ].Status = LightStatus.RED;
                        Lights[ 0 ].SetTime( time_flash + 1 - 1 );
                        Lights[ 2 ].SetTime( time_flash + 1 );
                        count = 8;
                        break;
                    case 8:
                        Lights[ 0 ].Status = LightStatus.RED;
                        Lights[ 1 ].Status = LightStatus.STRAIGHT;
                        Lights[ 2 ].Status = LightStatus.RED;
                        Lights[ 3 ].Status = LightStatus.STRAIGHT;
                        Lights[ 0 ].SetTime( time_red + 1 - 1 );
                        Lights[ 1 ].SetTime( time_left + 1 );
                        Lights[ 2 ].SetTime( time_red + 1 );
                        Lights[ 3 ].SetTime( time_left + 1 );
                        count = 9;
                        break;
                    case 9:
                        Lights[ 0 ].Status = LightStatus.RED;
                        Lights[ 1 ].Status = LightStatus.STRAIGHT_FLASH;
                        Lights[ 2 ].Status = LightStatus.RED;
                        Lights[ 3 ].Status = LightStatus.STRAIGHT_FLASH;
                        Lights[ 1 ].SetTime( time_flash + 1 );
                        Lights[ 3 ].SetTime( time_flash + 1 );
                        count = 10;
                        break;
                    case 10:
                        Lights[ 0 ].Status = LightStatus.RED;
                        Lights[ 1 ].Status = LightStatus.YELLOW;
                        Lights[ 2 ].Status = LightStatus.RED;
                        Lights[ 3 ].Status = LightStatus.YELLOW;
                        Lights[ 1 ].SetTime( time_yellow + 1 );
                        Lights[ 3 ].SetTime( time_yellow + 1 );
                        count = 11;
                        break;
                    case 11:
                        Lights[ 0 ].Status = LightStatus.RED;
                        Lights[ 1 ].Status = LightStatus.YELLOW_FLASH;
                        Lights[ 2 ].Status = LightStatus.RED;
                        Lights[ 3 ].Status = LightStatus.YELLOW_FLASH;
                        Lights[ 1 ].SetTime( time_flash + 1 );
                        Lights[ 3 ].SetTime( time_flash + 1 );
                        count = 12;
                        break;
                    case 12:
                        Lights[ 0 ].Status = LightStatus.RED;
                        Lights[ 1 ].Status = LightStatus.LEFT;
                        Lights[ 2 ].Status = LightStatus.RED;
                        Lights[ 3 ].Status = LightStatus.LEFT;
                        Lights[ 1 ].SetTime( time_left + 1 );
                        Lights[ 3 ].SetTime( time_left + 1 );
                        count = 13;
                        break;
                    case 13:
                        Lights[ 0 ].Status = LightStatus.RED;
                        Lights[ 1 ].Status = LightStatus.LEFT_FLASH;
                        Lights[ 2 ].Status = LightStatus.RED;
                        Lights[ 3 ].Status = LightStatus.LEFT_FLASH;
                        Lights[ 1 ].SetTime( time_flash + 1 );
                        Lights[ 3 ].SetTime( time_flash + 1 );
                        count = 0;
                        break;
                }

            }
            
                
            
            /*if ( i == 3 )
            {
                Lights[ 0 ].Status = LightStatus.STRAIGHT;
                Lights[ 1 ].Status = LightStatus.RED;
                Lights[ 2 ].Status = LightStatus.STRAIGHT;
                Lights[ 3 ].Status = LightStatus.RED;
                Lights[ 0 ].SetTime( time_left );
                Lights[ 1 ].SetTime( time_left );
                Lights[ 2 ].SetTime( time_left );
                Lights[ 3 ].SetTime( time_left );

            }

            else if ( i == 0 )
            {
                Lights[ 0 ].Status = LightStatus.LEFT;
                Lights[ 1 ].Status = LightStatus.RED;
                Lights[ 2 ].Status = LightStatus.LEFT;
                Lights[ 3 ].Status = LightStatus.RED;
                Lights[ 0 ].SetTime( time_left );
                Lights[ 1 ].SetTime( time_left );
                Lights[ 2 ].SetTime( time_left );
                Lights[ 3 ].SetTime( time_left );

            }

            else if ( i == 1 )
            {
                Lights[ 0 ].Status = LightStatus.RED;
                Lights[ 1 ].Status = LightStatus.STRAIGHT;
                Lights[ 2 ].Status = LightStatus.RED;
                Lights[ 3 ].Status = LightStatus.STRAIGHT;
                Lights[ 0 ].SetTime( time_left );
                Lights[ 1 ].SetTime( time_left );
                Lights[ 2 ].SetTime( time_left );
                Lights[ 3 ].SetTime( time_left );

            }

            else
            {
                Lights[ 0 ].Status = LightStatus.YELLOW;
                Lights[ 1 ].Status = LightStatus.YELLOW;
                Lights[ 2 ].Status = LightStatus.YELLOW;
                Lights[ 3 ].Status = LightStatus.YELLOW;
                Lights[ 0 ].SetTime( time_left );
                Lights[ 1 ].SetTime( time_left );
                Lights[ 2 ].SetTime( time_left );
                Lights[ 3 ].SetTime( time_left );
            }*/
        }


    }
}